//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 10
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace CreateNewMeetingAndInviteAttendees
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO: You must set up the time zone information and attendees when calling 
            // CreateNewMeetingAndInviteAttendees
        }

        /// <summary>
        /// Creates a new meeting and sends invitations to all attendees (Listing 10-2)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <param name="meetingStartTime">Start time for the meeting</param>
        /// <param name="meetingEndTime">End time for the meeting</param>
        /// <param name="meetingTimeZone">Time zone for the meeting</param>
        /// <param name="requiredAttendees">All required attendees</param>
        /// <param name="optionalAttendees">All optional attendees</param>
        /// <param name="meetingRoom">Meeting room location</param>
        /// <returns>Id of newly created meeting</returns>
        /// 
        public static ItemIdType CreateNewMeetingAndInviteAttendees(
            ExchangeServiceBinding binding,
            DateTime meetingStartTime,
            DateTime meetingEndTime,
            TimeZoneType meetingTimeZone,
            EmailAddressType[] requiredAttendees,
            EmailAddressType[] optionalAttendees,
            EmailAddressType meetingRoom)
        {
            // Basic new calendar item information
            //
            CalendarItemType newCalendarItem = new CalendarItemType();
            newCalendarItem.Start = meetingStartTime;
            newCalendarItem.StartSpecified = true;
            newCalendarItem.End = meetingEndTime;
            newCalendarItem.EndSpecified = true;
            newCalendarItem.MeetingTimeZone = meetingTimeZone;

            // Set the location of the meeting to be the name of the meeting room
            //
            newCalendarItem.Location = meetingRoom.Name;
            newCalendarItem.Subject = "Meeting in room " + meetingRoom.Name;

            // Create an Attendee instance for each EmailAddreesType instance and add
            // is to the appropriate collection
            //
            newCalendarItem.RequiredAttendees = new
                AttendeeType[requiredAttendees.Length];
            for (int x = 0; x < requiredAttendees.Length; x++)
            {
                newCalendarItem.RequiredAttendees[x] = new AttendeeType();
                newCalendarItem.RequiredAttendees[x].Mailbox = requiredAttendees[x];
            }

            newCalendarItem.OptionalAttendees = new
                AttendeeType[optionalAttendees.Length];
            for (int x = 0; x < optionalAttendees.Length; x++)
            {
                newCalendarItem.OptionalAttendees[x] = new AttendeeType();
                newCalendarItem.OptionalAttendees[x].Mailbox = optionalAttendees[x];
            }

            newCalendarItem.Resources = new AttendeeType[1];
            newCalendarItem.Resources[0] = new AttendeeType();
            newCalendarItem.Resources[0].Mailbox = meetingRoom;

            // Prepare the create item request
            //
            CreateItemType createItemRequest = new CreateItemType();
            createItemRequest.SendMeetingInvitations =
                CalendarItemCreateOrDeleteOperationType.SendOnlyToAll;
            createItemRequest.SendMeetingInvitationsSpecified = true;
            createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
            createItemRequest.Items.Items = new CalendarItemType[] { newCalendarItem };

            // Call the CreateItem web method and check for success.
            // If successful, return the id of the new meeting
            //
            CreateItemResponseType response = binding.CreateItem(createItemRequest);
            ItemInfoResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as
                  ItemInfoResponseMessageType;

            if (responseMessage.ResponseCode != ResponseCodeType.NoError)
            {
                throw new Exception(String.Format(
                    "Unable to create a new meeting\r\n{0}\r\n{1}",
                    responseMessage.ResponseCode,
                    responseMessage.MessageText));
            }
            return responseMessage.Items.Items[0].ItemId;
        }

    }
}
